package com.lanou3g.k12.quartz;

import com.lanou3g.k12.entity.Coupon;
import com.lanou3g.k12.entity.Orders;
import com.lanou3g.k12.mapper.CouponCodeMapper;
import com.lanou3g.k12.mapper.CouponMapper;
import com.lanou3g.k12.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

@Component
public class CouponJob {
@Autowired
private CouponMapper couponMapper;
@Autowired
private OrdersMapper ordersMapper;
@Autowired
private CouponCodeMapper couponCodeMapper;
    public void run(){
        //获取当前时间变为毫秒数
        long nowTime = new Date().getTime();
        //查询兑换券表的所有字段
        List<Coupon> allCoupon = couponMapper.selectCoupon();
        //获取兑换券的结束时间
        for (Coupon coupon : allCoupon) {
            //如果没有兑换券结束时间就继续循环
            if(coupon.getEndTime()==null){
                continue;
            }
            long endTime = coupon.getEndTime().getTime();
            Long id = coupon.getId();
            //如果过期，状态标记为失效，并把兑换券库的cpid变为空
            if(endTime<=nowTime){
                couponMapper.updateStatus(id);
                couponCodeMapper.updateByCpId(id);
            }
        }
    }
    /**
     * 设置一个任务 每30分钟执行一次 任务为获取所有未支付订单 判断是否失效
     */
    public void run1(){
        long nowTime = new Date().getTime();
        long i = 18000000;
        List<Orders> orders = ordersMapper.SelectOrders();
        for (Orders order : orders) {
                Long id = order.getId();
                if ((nowTime-order.getCreateTime().getTime()-i)>0){
                    ordersMapper.updateExpire(id);
                    continue;
                }
        }
    }
}
